//
//  UserViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "UserViewController.h"
#import "CollectViewController.h"
#import "IdeaViewController.h"
#import "Model.h"
#import "Fit.h"



@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, retain)NSArray *array;
@property(nonatomic, assign)NSInteger flag;
@end

@implementation UserViewController
-(void)dealloc
{
    [_tableView release];
    [_imageView release];
    [_array release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    self.title = @"设置";
    _flag = 0;
    [self changeColor];
    
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [_tableView reloadData];
}

-(void)loadView
{
    [super loadView];
    self.array = @[@"我的收藏", @"清除缓存", @"模式切换", @"意见反馈", @"关于我们"];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.tableFooterView = [[UIView alloc]init];
}


#pragma mark - 计算单个文件大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}
#pragma mark - 计算文件夹大小的方法
-(float)folderSizeAtPath:(NSString *)path
{
    //创建文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumerator = [[manager subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    NSString *fileName = [NSString string];
    //文件夹大小
    float folderSize = 0;
    while ((fileName = [enumerator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];//字符串拼接时加/
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if (indexPath.row == 0) {
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        cell.textLabel.text = _array[0];
        cell.textLabel.textColor = [Model shareInstance].TitleColor;
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        return cell;
    }
    else if (indexPath.row == 1) {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
        NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
        cell.textLabel.text = _array[1];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:cachePath]];
        cell.detailTextLabel.textColor = [Model shareInstance].TitleColor;
        cell.textLabel.textColor = [Model shareInstance].TitleColor;
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        return cell;
    }
    else if (indexPath.row == 2) {
        cell.textLabel.text = _array[2];
        cell.textLabel.textColor = [Model shareInstance].TitleColor;
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        return cell;
    }
    else if (indexPath.row == 3) {
        cell.textLabel.text = _array[3];
        cell.textLabel.textColor = [Model shareInstance].TitleColor;
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        return cell;
    }
    cell.textLabel.text = _array[4];
    cell.textLabel.textColor = [Model shareInstance].TitleColor;
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:{
            CollectViewController *collectVC = [[CollectViewController alloc]init];
            [self.navigationController pushViewController:collectVC animated:YES];
        }
            break;
        case 1:{
            //获取完整路径
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            path = [path stringByAppendingPathComponent:@"Caches"];
            NSFileManager *manager = [NSFileManager defaultManager];
            if ([manager fileExistsAtPath:path]) {
                NSArray *fileNameArray = [manager subpathsAtPath:path];
                for (NSString *fileName in fileNameArray) {
                    //拼接绝对路径
                    NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
                    //通过文件管理者删除文件
                    [manager removeItemAtPath:absolutePath error:nil];
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"缓存已被清理干净了哦~"preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];

                }
            }
            [_tableView reloadData];
        }
            break;
        case 2:{
            if (_flag == 1) {
                [[Model shareInstance] changeColor:_flag - 1];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[NSNumber numberWithInteger:_flag]];
                [[NSUserDefaults standardUserDefaults] setInteger:_flag - 1 forKey:@"night"];
                _flag = 0;
            }
            else if (_flag == 0) {
                [[Model shareInstance] changeColor:_flag + 1];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[NSNumber numberWithInteger:_flag]];
                [[NSUserDefaults standardUserDefaults] setInteger:_flag + 1 forKey:@"night"];
                _flag = 1;
            }
            [self changeColor];

        }
            break;
        case 3:{
            IdeaViewController *ideaVC = [[IdeaViewController alloc]init];
            [self.navigationController pushViewController:ideaVC animated:YES];
        }
            break;
        case 4:{
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"暂无信息"preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
